package webserver;

import gui.GUI;
import gui.MyJTextPane;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;

public class Control implements ActionListener, ItemListener {
	private static Control instance;
	private Server server = null;
	private String host;
	private int port;
	private String contentBase = null;
	GUI gui;

	private Control() {
	};

	public static Control getInstance() {
		if (instance == null) {
			instance = new Control();
		}
		return instance;
	}

	public void doStart() throws IOException {
		server = new Server(host, port, contentBase);
		Thread thread = new Thread(server);
		thread.start();
	}

	public void setHost(String host) {
		this.host = host;
	}

	public void setPort(int port) {
		if (server == null || !server.isAlive()) {
			this.port = port;
		}
	}

	public void setContentBase(String contentBase) {
		if (server == null || !server.isAlive()) {
			this.contentBase = contentBase;
		}
	}

	public String getContentBase() {
		return contentBase;
	}

	public void doStop() throws IOException {
		if (server != null)
			server.close();
			server = null;
	}

	public void log(Logger logger, String message) {
		//System.out.println("[" + logger.getID() + "] " + message);
		if (gui == null) {
			System.out.println("[" + logger.getID() + "] " + message);
		} else {
			MyJTextPane textPane = gui.getTextPaneForID(logger.getID());
			textPane.append(message);
		}
	}

	public void startGUI() {
		gui = new GUI();
	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// No need to check e's ID, startButton is the only button.
		int port = gui.getPort();
		File contentBase = gui.getContentBase();

		if (server != null) {
			try {
				doStop();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			JButton btn = (JButton)e.getSource();
			btn.setBackground(Color.GREEN);
			btn.setText("start");
		} else if ((port >= 0 && port <= 65535) && contentBase.isDirectory()) {
			Control.getInstance().setHost(gui.getHost());
			Control.getInstance().setPort(port);
			Control.getInstance().setContentBase(contentBase.getAbsolutePath());

			try {
				Control.getInstance().doStart();

				System.out.println("Starting...");
				
				JButton btn = (JButton)e.getSource();
				btn.setBackground(Color.RED);
				btn.setText("stop");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} else {
			System.out
					.println("Invalid input. Expected port number between 0 and 65535. ContentBase should be a valid directory.");
		}
	}

	public String[] getPossibleHosts() {
		String[] hosts;
		InetAddress[] ip = null;
		try {
			ip = InetAddress.getAllByName(InetAddress.getLocalHost()
					.getHostName());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		hosts = new String[ip.length];
		for (int i = 0; i < ip.length; i++) {
			hosts[i] = ip[i].getHostAddress();
		}

		return hosts;
	}
}
